package com.jiyinit.web.core.video.manager;

import com.jiyinit.core.framework.base.entity.ResponseDto;
import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.core.framework.base.util.Check;
import com.jiyinit.web.core.video.dto.MroleUserRequest;
import com.jiyinit.web.core.video.entity.TabCoreMroleUserEntity;
import com.jiyinit.web.core.video.service.MroleUserServiceImpl;
import com.jiyinit.web.core.video.vo.MobilRoleUserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * Created by user1 on 2018/12/14.
 */
@Controller
@RequestMapping(value = "/mroleUser")
public class MobileRoleUserController {

    private static Logger logger = LoggerFactory.getLogger(MobileRoleUserController.class);

    @Resource(name = "core.mroleUserServiceImpl")
    private MroleUserServiceImpl service;

    /**
     * 列表页
     * @return
     */
    @RequestMapping(value = "/roleUser", method = RequestMethod.GET)
    public String toIndex(){
        return "mobile/roleUser";
    }

    /**
     * 用户分配角色页面
     * @return
     */
    @RequestMapping(value = "/toAdd", method = RequestMethod.GET)
    public String toAddPage(){
        return "mobile/roleUserAdd";
    }

    /**
     * 用户更改角色页面
     * @return
     */
    @RequestMapping(value = "/toUpdate", method = RequestMethod.GET)
    public String toUpdatePage(HttpServletRequest req){
        try {
            req.setAttribute("id", req.getParameter("id"));
            req.setAttribute("roleName", URLDecoder.decode(req.getParameter("roleName"), "UTF-8"));
            req.setAttribute("userName", URLDecoder.decode(req.getParameter("userName"), "UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "mobile/roleUserUpdate";
    }

    /**
     * 选择用户页面
     * @return
     */
    @RequestMapping(value = "/toSelectUsers", method = RequestMethod.GET)
    public String toSelectUsers(){
        return "mobile/selectUsers";
    }

    /**
     * 选择用户页面
     * @return
     */
    @RequestMapping(value = "/toSelectRoles", method = RequestMethod.GET)
    public String toSelectRoles() {
        return "mobile/selectRoles";
    }


    /**
     * 分页查询
     * @param req
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/roleUser", method = RequestMethod.POST)
    public PagingResult<MobilRoleUserVo> roleUser(HttpServletRequest req, MroleUserRequest request){
        //request = JsonEntityTransform.json2Object(request_json, MroleUserRequest.class);
        logger.info("转换为obj之后的参数: {}", request.toString());
        request.setLimit(10);
        PagingResult<MobilRoleUserVo> pag = service.findAllBySelective(request);
        return pag;
    }

    /**
     * 给用户分配角色
     * @param userId
     * @param roleId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addUserRole")
    public ResponseEntity<ResponseDto> addUserRole(Integer userId, Integer roleId){
        if(Check.isNullObj(userId) || Check.isNullObj(roleId)){
            return new ResponseEntity<ResponseDto>(ResponseDto.responseFail("参数错误"), HttpStatus.OK);
        }
        TabCoreMroleUserEntity obj = new TabCoreMroleUserEntity();
        obj.setUserId(userId);
        obj.setMroleId(roleId);
        int addNum = service.add(obj);

        if(addNum > 0){
            return new ResponseEntity<ResponseDto>(ResponseDto.responseOK(""), HttpStatus.OK);
        }
        return new ResponseEntity<ResponseDto>(ResponseDto.responseFail("系统未知异常"), HttpStatus.OK);
    }

    /**
     * 修改用户的角色
     * @param roleId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateUserRole")
    public ResponseEntity<ResponseDto> updateUserRole(Integer id, Integer roleId){
        if(Check.isNullObj(id) || Check.isNullObj(roleId)){
            return new ResponseEntity<ResponseDto>(ResponseDto.responseFail("参数错误"), HttpStatus.OK);
        }
        TabCoreMroleUserEntity obj = new TabCoreMroleUserEntity();
        obj.setId(id);
        obj.setMroleId(roleId);
        int i = service.updateByUserId(obj);
        if(i > 0){
            return new ResponseEntity<ResponseDto>(ResponseDto.responseOK(""), HttpStatus.OK);
        }
        return new ResponseEntity<ResponseDto>(ResponseDto.responseFail("系统未知异常"), HttpStatus.OK);
    }

    /**
     * 删除
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete")
    public ResponseEntity<ResponseDto> delete(Integer[] ids){
        if(Check.isNullObject(ids)){
            return new ResponseEntity<ResponseDto>(ResponseDto.responseFail("参数错误"), HttpStatus.OK);
        }
        int num = 0;
        for (int i = 0; i < ids.length; i++) {
            num += service.delete(ids[i]);
        }

        if(num > 0){
            return new ResponseEntity<ResponseDto>(ResponseDto.responseOK(""), HttpStatus.OK);
        }
        return new ResponseEntity<ResponseDto>(ResponseDto.responseFail("系统未知异常"), HttpStatus.OK);
    }



}
